package com.isoft.c2team3service2.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isoft.c2team3service2.entity.ArtistPrivatePost;
import com.isoft.c2team3service2.entity.UserDTO;
import com.isoft.c2team3service2.mapper.ArtistMapper;
import com.isoft.c2team3service2.mapper.ArtistPrivatePostMapper;
import com.isoft.c2team3service2.mapper.UserFollowUserMapper;
import com.isoft.c2team3service2.service.ArtistPrivatePostService;
import com.isoft.c2team3service2.service.SensitiveWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ArtistPrivatePostServiceImpl extends ServiceImpl<ArtistPrivatePostMapper, ArtistPrivatePost> implements ArtistPrivatePostService {

    @Autowired
    private ArtistPrivatePostMapper privatePostMapper;

    @Autowired
    private SensitiveWordService sensitiveWordService;

    @Autowired
    private ArtistMapper artistMapper;

    @Autowired
    private UserFollowUserMapper userFollowUserMapper;

    /**
     * 发送私密帖子给艺人（需校验关注关系）
     * @return 帖子ID（成功）/ null（失败）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer sendPrivatePostToArtist(ArtistPrivatePost post, UserDTO currentUser) {
        Integer userId = currentUser.getId();
        Integer artistId = artistMapper.selectUserId(post.getArtistId());

        // 1. 校验用户是否关注该艺人
        int followCount = userFollowUserMapper.checkFollowStatus(userId, artistId);
        if (followCount == 0) {
            return null; // 未关注，无法发送
        }

        // 2. 填充帖子信息
        post.setUserId(userId);
        post.setCreateTime(LocalDateTime.now());
        post.setUpdateTime(LocalDateTime.now());

        String filteredContent = sensitiveWordService.filterText(post.getContent());
        String filterTitle = sensitiveWordService.filterText(post.getTitle());

        boolean containsContent = filteredContent.endsWith("（敏感词）");
        boolean containsTitle = filterTitle.endsWith("（敏感词）");

        if (containsContent || containsTitle) {
            return 0;
        }else {
            post.setContent(filteredContent);
            post.setTitle(filterTitle);
            // 3. 保存帖子并返回ID
            boolean success = save(post);
            return success ? post.getId() : null;
        }
    }

    @Override
    public List<ArtistPrivatePost> getUserPostsToArtist(Integer userId) {
        return privatePostMapper.selectByUserAndArtist(userId);
    }

    @Override
    public List<ArtistPrivatePost> getArtistReceivedPosts(Integer artistId) {
        return privatePostMapper.selectByArtistId(artistId);
    }

    @Override
    public boolean hasPermission(Integer postId, Integer userId) {
        // 校验用户是否为帖子发送者或艺人本人
        int count = privatePostMapper.checkPermission(postId, userId);
        return count > 0;
    }
    /**
     * 实现：根据艺人ID查询收到的私密帖子
     */
    @Override
    public List<ArtistPrivatePost> getPostsByArtistId(Integer artistId) {
        if (artistId == null) {
            return List.of();
        }
        // 假设 ArtistPrivatePost 表中有 artist_id 字段，关联艺人ID
        return lambdaQuery()
                .eq(ArtistPrivatePost::getArtistId, artistId) // 关联目标艺人
                .orderByDesc(ArtistPrivatePost::getCreateTime)
                .list();
    }
}